﻿using SuperMap.Data;
using SuperMap.Realspace;
using System;
using System.Data;
using System.Windows.Forms;
using Ycjw.PipeLine.Map.Basic.Conditions;
using Ycjw.PipeLine.Map.Tools.Conditions.JJAnalysts;

namespace Ycjw.PipeLine.Map.Tools.Conditions
{
    /// <summary>
    /// 碰撞分析
    /// </summary>
    public class PZAnalystToolBar: BasicToolBar
    {
        private readonly double STANDARD_V = 100;
        private readonly double STANDARD_H = 1.0;
        public PZAnalystToolBar()
        {
            this.ConditionPanelName = "碰撞分析";
            var selectToolStripButton = new ToolStripButton()
            {
                DisplayStyle = ToolStripItemDisplayStyle.ImageAndText,
                Text = "碰撞分析",
                Name = "Analyst"
            };
            base.ToolStrip.Items.Add(selectToolStripButton);

            selectToolStripButton.Click += SelectToolStripButton_Click;
        }
        public override void AfterClose()
        {
            this.SceneControl.Scene.TrackingLayer.Clear();
        }
        private void SelectToolStripButton_Click(object sender, EventArgs e)
        {
            this.SceneControl.Scene.TrackingLayer.Clear();
            var selected3Ds = base.SceneControl.Scene.FindSelection(true);
            if(selected3Ds.Length == 0)
            {
                Sunny.UI.UIMessageTip.Show("请选中一个管线对象");
                return;
            }
            var recordset = selected3Ds[0].ToRecordset();
            if(!(recordset.GetGeometry() is GeoLine3D))
            {
                Sunny.UI.UIMessageTip.Show("请选中一个管线对象");
                return;
            }
            GeoLine3D line = recordset.GetGeometry() as GeoLine3D;
            DataTable dt = new DataTable();
            dt.Columns.Add("图层");
            dt.Columns.Add("SMID");
            dt.Columns.Add("水平净距");
            dt.Columns.Add("水平净距-国标");

            dt.Columns.Add("垂直净距");
            dt.Columns.Add("垂直净距-国标");
            foreach (Layer3D lyr in this.Scene.Layers)
            {
                if (!(lyr is Layer3DDataset)) continue;
                Layer3DDataset lyr3D = lyr as Layer3DDataset;
                if (!(lyr3D.Dataset is DatasetVector)) continue;
                DatasetVector dsv = lyr3D.Dataset as DatasetVector;
                if (dsv.Type != DatasetType.Network3D) continue;
                var fs = dsv.GetAllFeatures();
                foreach(var f in fs.Values)
                {
                    var fLine = f.GetGeometry() as GeoLine3D;
                    if (Geometrist.Distance(fLine, line) == 0) continue;

                    var nearestPoints = DistBetweenLines.NearestPoints(fLine, line);
                    double getZ(GeoLine3D lineObj)
                    {
                        return lineObj[0][0].Z;
                    }
                    if (nearestPoints != null && nearestPoints.Length > 1)
                    {
                        double z1 = double.IsNaN(nearestPoints[0].Z) ? getZ(fLine) : nearestPoints[0].Z;
                        double z2 = double.IsNaN(nearestPoints[1].Z) ? getZ(line) : nearestPoints[1].Z;

                        var p1 = new Point3D() { X = nearestPoints[0].X, Y = nearestPoints[0].Y, Z = z1 };
                        var p2 = new Point3D() { X = nearestPoints[1].X, Y = nearestPoints[1].Y, Z = z2 };
                        Point3Ds ps = new Point3Ds(new Point3D[] { p1, p2 });
                        GeoLine3D gl3 = new GeoLine3D(ps);

                        var p1H = new Point3D() { X = nearestPoints[0].X, Y = nearestPoints[0].Y, Z = 0 };
                        var p2H = new Point3D() { X = nearestPoints[1].X, Y = nearestPoints[1].Y, Z = 0 };
                        Point3Ds psH = new Point3Ds(new Point3D[] { p1H, p2H });
                        GeoLine3D gl3H = new GeoLine3D(psH);

                        var hLength = Geometrist.ComputeLength(gl3H, new PrjCoordSys(4326));
                        var vLength = Math.Abs(p1.Z - p2.Z);
                        var realDis = Math.Sqrt(vLength * vLength + hLength * hLength);
                        if (realDis > STANDARD_H && realDis > STANDARD_V) continue;

                        if (hLength < STANDARD_H || vLength < STANDARD_V)
                        {
                            fLine.Style3D = new GeoStyle3D()
                            {
                                AltitudeMode = AltitudeMode.Absolute,
                                LineColor = System.Drawing.Color.Yellow,
                                LineWidth = 4
                            };
                            this.SceneControl.Scene.TrackingLayer.Add(fLine,string.Empty);
                            var dr = dt.NewRow();
                            dr[0] = lyr.Caption;
                            dr[1] = f.GetString("SMID");
                            if (hLength < STANDARD_H)
                            {
                                dr[4] = hLength;
                            }
                            dr[5] = STANDARD_H;
                            if (vLength < STANDARD_V)
                            {
                                dr[2] = vLength;
                            }
                            dr[3] = STANDARD_V;
                            dt.Rows.Add(dr);
                        }
                    }
                }
            }
            if(dt.Rows.Count == 0)
            {
                dt = null;
            }
            this.ShowResult(dt,this.DockPanel,this.ConditionPanelName);
        }


    }
}
